#include "stdafx.h"

//OLED任务
#define OLED_TASK_PRIO 7
#define OLED_STK_SIZE 256
OS_STK OLED_TASK_STK[OLED_STK_SIZE];
void oled_task(void *pdata){
	char temp[22];
	short tem;
	u8 epr[22]={0},time,flag=3;
	AT24CXX_Read(234,epr,21);
	while(1){
		if(flag>=3){
			if(time!=calendar.sec){
				time=calendar.sec;
				sprintf(temp,"%04d/%02d/%02d %d",calendar.w_year,calendar.w_month,calendar.w_date,calendar.week);
				OLED_ShowString(0,24,(u8 *)temp,12);
				sprintf(temp,"%02d:%02d:%02d",calendar.hour,calendar.min,calendar.sec);
				OLED_ShowString(0,36,(u8 *)temp,12);
				OLED_Refresh_Gram();
				flag=0;
				LED2=!LED2;
			}else{
				flag=3;
				delay_ms(10);
			}
		}else{
			tem=(short)(SHT2x_GetTempPoll()*100);
			sprintf(temp,"TEM:%02d.%02dC",tem/100,tem%100);
			tem=Get_Temprate();
			sprintf(temp,"%s TEM:%02d.%02dC",temp,tem/100,tem%100);
			OLED_ShowString(0,0,(u8 *)temp,12);
			tem=(short)(SHT2x_GetHumiPoll()*100);
			sprintf(temp,"HUM:%02d.%02d%%",tem/100,tem%100);
			OLED_ShowString(0,12,(u8 *)temp,12);
			OLED_ShowString(0,47,epr,12);
			OLED_Refresh_Gram();
			flag++;
		}
	}
}

//LED任务
#define LED_TASK_PRIO 11
#define LED_STK_SIZE 32
OS_STK LED_TASK_STK[LED_STK_SIZE];
void led_task(void *pdata){
	while(1){
		LED3=!LED3;
		delay_ms(200);
		LED4=!LED4;
		delay_ms(200);
		LED5=!LED5;
		delay_ms(200);
	}
}

//看门狗任务
#define WDG_TASK_PRIO 9
#define WDG_STK_SIZE 32
OS_STK WDG_TASK_STK[WDG_STK_SIZE];
void wdg_task(void *pdata){
	while(1){
		IWDG_Feed();
		delay_ms(250);
	}
}

//起始任务
#define START_TASK_PRIO 10
#define START_STK_SIZE 64
OS_STK START_TASK_STK[START_STK_SIZE];
void start_task(void *pdata){
	OS_CPU_SR cpu_sr=0;
	pdata=pdata;
	OS_ENTER_CRITICAL();//进入临界区
 	OSTaskCreate(led_task,(void *)0,(OS_STK*)&LED_TASK_STK[LED_STK_SIZE-1],LED_TASK_PRIO);
	OSTaskCreate(wdg_task,(void *)0,(OS_STK*)&WDG_TASK_STK[WDG_STK_SIZE-1],WDG_TASK_PRIO);
 	OSTaskCreate(oled_task,(void *)0,(OS_STK*)&OLED_TASK_STK[OLED_STK_SIZE-1],OLED_TASK_PRIO);
	OS_EXIT_CRITICAL();//退出临界区
	OSTaskSuspend(START_TASK_PRIO);//挂起
}

void task_start(){
	OSInit();
	OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO);
	OSStart();
}
